/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.test.web;

import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.webbase.common.config.Global;
import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.web.BaseController;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.test.entity.SysTest;
import com.foreveross.webbase.test.service.SysTestService;

/**
 * 测试管理Controller
 * @author yangwanli
 * @version 2017-03-08
 */
@Controller
@RequestMapping(value = "${adminPath}/test/sysTest")
public class SysTestController extends BaseController {

	@Autowired
	private SysTestService sysTestService;
	
	@RequiresPermissions("test:sysTest:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysTest sysTest) {
		Page<SysTest> page = sysTestService.findPage(new Page<SysTest>(request(), response()), sysTest); 
		attr("page", page);
		return "test/sysTestList";
	}

	@RequiresPermissions("test:sysTest:view")
	@RequestMapping("form")
	public String form(String id) {
		if(StringUtils.isNotEmpty(id)) {
			SysTest sysTest=sysTestService.get(id);
			attr("sysTest", sysTest);
		} else {
			attr("sysTest", new SysTest());
		}
		return "test/sysTestForm";
	}

	@RequiresPermissions("test:sysTest:edit")
	@RequestMapping("save")
	public String save(SysTest sysTest, RedirectAttributes redirectAttributes) {
		if (!beanValidator(redirectAttributes, sysTest)){
			return form(sysTest.getId());
		}
		if(sysTest.getIsNewRecord()){
			sysTest.setCreatime(new Date());
		}
		sysTestService.save(sysTest);
		addMessage(redirectAttributes, "保存TEST管理成功");
		return "redirect:"+Global.getAdminPath()+"/test/sysTest/?repage";
	}
	
	@RequiresPermissions("test:sysTest:edit")
	@RequestMapping("delete")
	public String delete(@RequestParam String id, RedirectAttributes redirectAttributes) {
		sysTestService.delete(id);
		addMessage(redirectAttributes, "删除TEST管理成功");
		return "redirect:"+Global.getAdminPath()+"/test/sysTest/?repage";
	}

}